package com.kaishengit.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.kaishengit.dao.core.BaseDao;
import com.kaishengit.dao.core.Page;
import com.kaishengit.pojo.Contact;
import com.kaishengit.pojo.FirstWordTag;
import com.kaishengit.pojo.Tag;
import com.kaishengit.pojo.User;

public class TagDao extends BaseDao<Tag,String> {
	
	@SuppressWarnings("unchecked")
	public List<FirstWordTag> findForGroup(User user){
		ProjectionList projectionList = Projections.projectionList();  
		projectionList.add(Projections.groupProperty("firstword"));  
		
		Criteria c = getSession().createCriteria(Tag.class);
		c.add(Restrictions.eq("user", user));
		c.setProjection(projectionList);
		
		List<String> list =  c.list();
		List<FirstWordTag> tlist = new ArrayList<FirstWordTag>();
		for(String s:list){
			System.out.println(s+"@@@@@@@@@@@@@@@@@@");
			FirstWordTag f = new FirstWordTag();
			f.setFirstWord(s);
			ProjectionList projectionList1 = Projections.projectionList();  
			projectionList1.add(Projections.groupProperty("content"));  
			Criteria c2 = getSession().createCriteria(Tag.class);
			c2.add(Restrictions.eq("firstword", s));
			c2.setProjection(projectionList1);
			List<String> list2=c2.list();
			List<String> contentlist=new ArrayList<String>();
			for(String st:list2){
				System.out.println(st+"&&&&&&&&&&&&&&&&&&&&&");
				contentlist.add(st);
				Criteria c3 = getSession().createCriteria(Tag.class);
				c3.add(Restrictions.eq("content", st));
				List<Tag> taglist = c3.list();
				f.setList(taglist);
			}
			f.setContentlist(contentlist);
			tlist.add(f);
		}
		return tlist;
	}
	
	
}
